package com.iss.demo.controller;

import com.iss.demo.config.PrereadConfig;
import com.iss.demo.entity.sixCourse;
import com.iss.demo.entity.sixCourse;
import com.iss.demo.model.MyResponseData;
import com.iss.demo.model.sixCourseModel;
import com.iss.demo.service.sixCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sixCourse/")
public class sixCourseController {

        @Autowired
        private sixCourseService service;

        @RequestMapping("selectAll")
        public List<sixCourseModel> selectAll(HttpServletRequest request){
            List<sixCourseModel> list = service.selectAll();
            return list;
        }
        @RequestMapping("delete")
        public MyResponseData delete(Integer[] s_ids){
            Integer count = service.delete(s_ids);
            MyResponseData responseData = new MyResponseData();
            responseData.setErrorCode(0);
            responseData.setStatus("删除网课数量："+count);
            return responseData;
        }

        @RequestMapping("insertCourse")
        public MyResponseData insertCourse(sixCourse sixcourse){
            Integer count = service.insertCourse(sixcourse);
            MyResponseData responseData = new MyResponseData();
            responseData.setErrorCode(0);
            responseData.setStatus("添加了"+count+"个网课");
            return responseData;
        }

        @RequestMapping("updateCourse")
        public MyResponseData updateCourse(sixCourse sixcourse){
            Integer count = service.updateCourse(sixcourse);
            MyResponseData responseData = new MyResponseData();
            responseData.setErrorCode(0);
            responseData.setStatus("更新了"+count+"个网课");
            return responseData;
        }

        @RequestMapping("selectById")
        public MyResponseData selectById(Integer id){
            sixCourse sixcourse = service.selectById(id);
            MyResponseData responseData = new MyResponseData();
            responseData.setErrorCode(0);
            responseData.setStatus("success");
            responseData.setObject(sixcourse);
            return responseData;
        }
    @Autowired
    PrereadConfig config;

    @RequestMapping("upload")
    public MyResponseData upload(@RequestParam(value = "files", required = false) MultipartFile[] files, Integer s_id, String info, HttpServletRequest request) {

        MyResponseData responseData = new MyResponseData();
        if (files == null || files.length == 0) {
            responseData.setErrorCode(-1);
            responseData.setStatus("没有选择文件");
        }

        File path = new File(config.getUploadPath()+File.separator+s_id);
        if (!path.exists()) {
            path.mkdirs();
        }

        String surfaceName = "";
        for (int i = 0; i < files.length; i++) {
            String newFileName = System.currentTimeMillis() + "_" + files[i].getOriginalFilename();
            File newFile = new File(path, newFileName);   //保存文件
            if (i == 0) {
                surfaceName = "uploadFile"+File.separator+s_id+File.separator+newFileName;
            }
            try {
                FileOutputStream fos = new FileOutputStream(newFile);
                InputStream is = files[i].getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                is.close();
                responseData.setErrorCode(0);
                responseData.setStatus("上传成功");
                Integer count = service.updatesixCourseInfo(surfaceName, info, s_id);
            } catch (IOException e) {
                responseData.setErrorCode(-2);
                responseData.setStatus("上传失败");
                e.printStackTrace();
            }
        }
        return responseData;
    }

    @RequestMapping("getId")
    public MyResponseData getId(HttpSession session) {
        MyResponseData responseData = new MyResponseData();
        responseData.setObject(session.getAttribute("s_id"));
        return responseData;
    }

    @RequestMapping("getInfoById")
    public MyResponseData getInfoById(Integer s_id, HttpServletRequest request) {

        MyResponseData responseData = new MyResponseData();
//从相应的文件夹读取图片
        File path = new File(config.getUploadPath()+File.separator+s_id);
        File[] files = path.listFiles();
        String[] paths = new String[files.length];
        for (int i = 0; i < files.length; i++) {
            paths[i] = "uploadFile"+File.separator+s_id + File.separator + files[i].getName();
            System.out.println(paths[i]);
        }
        responseData.setObject(paths);
        sixCourse sixCourse = service.selectById(s_id);
        Map<String, Object> map = new HashMap<>();
        map.put("sixCourse", sixCourse);
        responseData.setObjectMap(map);
        return responseData;
    }
    }